package mymaggi.serienjunkiesmonitor;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

public class GetLinks {

	private static Logger logger = Logger.getRootLogger();
	private String quelltext;
	private List<String> episodes;

	GetLinks(String quell, List<String> eps) {
		quelltext = quell;
		episodes = eps;
	}

	public List<String> getLinks(String hoster) {
		logger.debug("Starte die Link-Suche.");
		List<String> links = new ArrayList<String>();
		try {
			for (int i = 0; i < episodes.size(); i++) {
				int currIndex = quelltext.indexOf(episodes.get(i));
				String firstFilter = quelltext.substring(currIndex, quelltext
						.indexOf(hoster, currIndex));
				String link = firstFilter.substring(firstFilter
						.lastIndexOf("<a href=") + 9, firstFilter
						.lastIndexOf("\" target="));
				links.add(episodes.get(i));
				links.add(link);
			}
		} catch (Exception e) {
			logger.error(e);
		}
		logger.debug("Links wurden gesucht.");
		return links;
	}
}
